package com.example.dynamicdatasource.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 省份数据源配置
 * 用于配置每个省份的数据库连接信息
 */
@Data
@Component
@ConfigurationProperties(prefix = "provinces")
public class ProvinceDbProperties {

    private Map<String, ProvinceDbConfig> configs = new HashMap<>();

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ProvinceDbConfig {
        private String name;
        private String description;
        private boolean needLazyLoad = true; // 默认需要懒加载
    }

    public ProvinceDbConfig getConfigsByName(String name) {
        return configs.get(name);
    }

    // 获取需要懒加载的数据源配置
    public Map<String, ProvinceDbConfig> getLazyLoadConfigs() {
        return configs.entrySet().stream()
                .filter(entry -> entry.getValue().isNeedLazyLoad())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    // 获取不需要懒加载的数据源配置
    public Map<String, ProvinceDbConfig> getEagerLoadConfigs() {
        return configs.entrySet().stream()
                .filter(entry -> !entry.getValue().isNeedLazyLoad())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }
}
